package com.augurit.gis.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * 将对象转化为sql时的辅助类
 * @author jianfeng.shen
 *
 */
public class SQLUtil {
	/**
	 * 日期转化为sql
	 * @param date
	 * @return
	 */
	public static String date2SqlString(Date date){
		if(date==null)return null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return "to_date('"+dateFormat.format(date)+"','yyyy-MM-dd')";
	}
	public static String date2MSSqlString(Date date){
		if(date==null)return null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return "cast('"+dateFormat.format(date)+"' as datetime)";
	}
	public static String getSqlDateString(){
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return "'"+dateFormat.format(date)+"'";
	}
	public static String array2SqlString(Number[] array){
		if(array==null||array.length==0) return "(-0.00001)";
		String in = "(";
		for(Number number:array){
			in+=number+",";
		}
		in = in.substring(0,in.length()-1)+")";
		return in;
	}
	public static String list2SqlString(List<? extends Number > list){
		if(list==null||list.size()==0) return "(-0.00001)";
		String in = "(";
		for(Number number:list){
			in+=number+",";
		}
		in = in.substring(0,in.length()-1)+")";
		return in;
	}
	
	/**
	 * 字符串转化为sql
	 * @param string
	 * @return
	 */
	public static String string2SqlString(String string){
		if(string==null||string.equals(""))return null;
		string = string.replace("'", "''");
		return "'"+string+"'";
	}
	/**
	 * 将字符中的"'"号转化为""";
	 * @param fieldValue
	 * @return
	 */
	public static String convertString4Sql(String fieldValue){
		if(fieldValue==null){
			return null;
		}else{
			fieldValue = fieldValue.replace("'", "''");
			return fieldValue;
		}
	}
	public static void main(String[] args) {
		System.out.println(getSqlDateString());
	}
}
